package com.wisdytech.linkdcs.equipLifeCycle.malfunction.service.impl;

import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MApplyRecord;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.model.MalfunctionRecord;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.dao.IMalfunctionRecordDao;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.dao.MApplyRecordDao;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.dto.MalfunctionSearchDto;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.service.IMApplyRecordService;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.vo.MalfunctionApplyVO;
import com.wisdytech.linkdcs.equipLifeCycle.malfunction.vo.MalfunctionVo;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenbin
 * @since 2018-07-25
 */
@Service("iMApplyRecordService")
@Transactional
public class MApplyRecordServiceImpl extends BaseService<MApplyRecordDao, MApplyRecord> implements IMApplyRecordService {

	@Resource
	private MApplyRecordDao mApplyRecordDao;
	@Resource
	private IMalfunctionRecordDao iMalfunctionRecordDao;
	@Override
	public boolean addApplyRecord(MApplyRecord applyRecord, String username) {
		//更新故障状态
		MalfunctionRecord record=new MalfunctionRecord();
		record.setPkId(applyRecord.getmRecordId());
		record.setFailureStatus("4");//更改为委外状态
		record.updateById();
		//插入委外记录
		applyRecord.setPkId(BaseUtil.getUUID());
		applyRecord.setApplicationDate(new Date());
		applyRecord.setApplicationStatus("0");//待审批状态
		mApplyRecordDao.insert(applyRecord);
		return true;
	}
	@Override
	public PageInfo<MalfunctionApplyVO> listApplyRecords(Filter filter, MalfunctionSearchDto dto) {
		List<MalfunctionApplyVO> result =
				this.beforeList(IMalfunctionRecordDao.class, filter).listApplyRecords(dto);
		
		return new PageInfo<>(result);
	}
	@Override
	public void applyCommitOver(String pkId) {
		EntityWrapper<MApplyRecord> ew = new EntityWrapper<>();
		ew.eq("M_RECORD_ID", pkId);
		MApplyRecord mApplyRecord = new MApplyRecord();
		mApplyRecord.setApplicationStatus("1");//改为已审批状态
        //修改为待返厂
        MalfunctionRecord malfunctionRecord = new MalfunctionRecord();
        malfunctionRecord.setPkId(pkId);
        malfunctionRecord.setFailureStatus("5");//改为待返厂
        malfunctionRecord.setUpdateDate(new Date());
        iMalfunctionRecordDao.updateById(malfunctionRecord);
		mApplyRecordDao.update(mApplyRecord, ew);
	}
	@Override
	public void applyReturnFactory(String pkId) {
		MalfunctionRecord malfunctionRecord = new MalfunctionRecord();
		malfunctionRecord.setPkId(pkId);
		malfunctionRecord.setFailureStatus("3");//改为完成
		malfunctionRecord.setUpdateDate(new Date());
		iMalfunctionRecordDao.updateById(malfunctionRecord);
		
	}

}
